//
//  WbNavigationController.swift
//  Weibo
//
//  Created by HH on 16/11/30.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit

class WbNavigationController: UINavigationController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.interactivePopGestureRecognizer?.delegate = self;
        // Do any additional setup after loading the view.
    }
    
    
    
    //跳转后调整视图效果
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        print(self.viewControllers.count)
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: true)
        
        print(self.viewControllers.count)
        
        if viewControllers.count > 1 {
            if viewControllers.count == 2 {
                let title = viewControllers.first!.title!
                
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, selector: #selector(returnAction(sender:)))
            }else{
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, selector: #selector(returnAction(sender:)))
            }
            
            viewController.navigationItem.title = "当前为第\(viewControllers.count)个控制器"
        }
        
    }
    
    @objc private func returnAction(sender:UIButton){
        
       _ =  popViewController(animated: true)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}


extension WbNavigationController : UIGestureRecognizerDelegate{
    
    //创建左边的按钮时,侧滑效果会被取消,使用这个方法重新开启
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1 {
            return false
        }
        
        return true
    }
}
